﻿
/***
	
	# CopyRights By Pr (yslee96@naver.com / webmaster@prev.kr) 
		
	@ Author Pr (yslee96@naver.com) 
	
	@ http://prev.kr
	
***/


package kr.prev.controls {
	
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.Screen;
	import flash.events.Event;
	import flash.display.NativeWindowType;
	import flash.display.Stage;
	import flash.display.DisplayObject;
	import flash.events.NativeWindowBoundsEvent;
	import flash.desktop.NativeApplication;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import flash.display.SimpleButton;
	import flash.media.Sound;
	
	public class Alert {
		
		
		[Embed(source="../source/CommonSource.swf", symbol='OKButton')]
		private static var OKButtonClass:Class
		
		[Embed(source="../source/AlertSound.mp3")]
		private static var AlertSoundClass:Class
		
		private static var alertSound:Sound = new AlertSoundClass() as Sound
		
		private static var alertWindow:NativeWindow
		private static var alertWindowOption:NativeWindowInitOptions
		private static var nativeWindow:NativeWindow
		private static var alertWinowActive:Boolean = false
		
		
		
		public static function alert(message:String, title:String = "경고"):void {
			if (NativeWindow.isSupported) {
				
				nativeWindow = NativeApplication.nativeApplication.openedWindows[0]
				
				alertWindowOption = new NativeWindowInitOptions()
					
				alertWindowOption.resizable = false
				alertWindowOption.type = NativeWindowType.NORMAL
				alertWindowOption.minimizable = false
				alertWindowOption.maximizable = false
				
				alertWindow = new NativeWindow(alertWindowOption)
				
				alertWindow.stage.align = StageAlign.TOP_LEFT
				alertWindow.stage.scaleMode = StageScaleMode.NO_SCALE
				
				alertWindow.orderInFrontOf( nativeWindow )
				
				alertWindow.width = 320
				alertWindow.height = 110
				
				alertWindow.x = (Screen.mainScreen.bounds.width - alertWindow.width) / 2
				alertWindow.y = (Screen.mainScreen.bounds.height - alertWindow.height) / 2
				alertWindow.title = title
				
				alertWindow.addEventListener(Event.CLOSING, alertWindowClosingHandler)
				
				nativeWindow.addEventListener(Event.ACTIVATE, stageWindowActivateHandler)
				nativeWindow.addEventListener(NativeWindowBoundsEvent.MOVING, stageWindowBoundsHandler)
				nativeWindow.addEventListener(NativeWindowBoundsEvent.RESIZING, stageWindowBoundsHandler)
				nativeWindow.addEventListener(Event.CLOSING, nativeWindowClosingHandler)
				
				var OKButton:SimpleButton = new OKButtonClass()
				OKButton.x = (alertWindow.width - OKButton.width) / 2
				OKButton.y = 50
				OKButton.addEventListener(MouseEvent.CLICK, OKButtonClickHandler)
				
				
				var txt:TextField = new TextField()
				txt.selectable = false
				txt.text = message
				txt.x = 10
				txt.y = 5
				txt.width = alertWindow.width - 20
				txt.height = alertWindow.height - OKButton.y - 10
				
				
				
				alertWindow.stage.addChild( OKButton )
				alertWindow.stage.addChild( txt )
				
				alertWindow.activate()
				
				alertWinowActive = true
				
			}else {
				trace('Native Window를 지원하지 않는 환경입니다.')
			}
		}
		
		private static function stageWindowActivateHandler(e:Event):void {
			if (alertWinowActive) {
				activateAlertWindow()
			}
		}
		
		private static function stageWindowBoundsHandler(e:NativeWindowBoundsEvent):void {
			if (alertWinowActive) {
				activateAlertWindow()
				e.preventDefault()
			}
		}
		
		private static function nativeWindowClosingHandler(e:Event):void {
			if (alertWinowActive) {
				activateAlertWindow()
				e.preventDefault()
			}
		}
		
		private static function activateAlertWindow():void {
			if (alertWinowActive) {
				alertSound.play()
				
				alertWindow.orderInFrontOf( nativeWindow )
				alertWindow.activate()
			}
		}
		
		private static function alertWindowClosingHandler(e:Event):void {
			alertWinowActive = false
			if (nativeWindow) {
				nativeWindow.removeEventListener(Event.ACTIVATE, stageWindowActivateHandler)
				nativeWindow.removeEventListener(NativeWindowBoundsEvent.MOVING, stageWindowBoundsHandler)
				nativeWindow.removeEventListener(NativeWindowBoundsEvent.RESIZING, stageWindowBoundsHandler)
				nativeWindow.removeEventListener(Event.CLOSING, nativeWindowClosingHandler)
			}
		}
		
		private static function OKButtonClickHandler(e:MouseEvent):void {
			alertWindow.close()
			alertWindowClosingHandler(null)
		}
		
	}
	
}
